package com.ruoyi.project.system.controller;

import java.util.Date;
import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TDrugs;
import com.ruoyi.project.system.domain.TNews;
import com.ruoyi.project.system.service.ITNewsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 新闻中心Controller
 * 
 * @author ruoyi
 * @date 2020-06-04
 */
@RestController
@RequestMapping("/news")
public class TNewsController extends BaseController
{
    @Autowired
    private ITNewsService tNewsService;

    /**
     * 查询新闻中心列表
     */
    //@PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(TNews tNews)
    {
        startPage();
        List<TNews> list = tNewsService.selectTNewsList(tNews);
        return getDataTable(list);
    }
    @GetMapping("/getNewsListHome")
    public TableDataInfo getNewsListHome(TNews tNews)
    {
    	startPage();
        List<TNews> list = tNewsService.selectTNewsList(tNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻中心列表
     */
   // @PreAuthorize("@ss.hasPermi('system:news:export')")
    @Log(title = "新闻中心", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TNews tNews)
    {
        List<TNews> list = tNewsService.selectTNewsList(tNews);
        ExcelUtil<TNews> util = new ExcelUtil<TNews>(TNews.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取新闻中心详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tNewsService.selectTNewsById(id));
    }

    /**
     * 新增新闻中心
     */
    //@PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "新闻中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TNews tNews)
    {
    	
    	tNews.setCreateUser(SecurityUtils.getLoginUser().getUser().getUserName());
    	tNews.setCreateDate(new Date());
    	tNews.setUpdateUser(SecurityUtils.getLoginUser().getUser().getUserName());
    	tNews.setUpdateDate(new Date());
        return toAjax(tNewsService.insertTNews(tNews));
    }

    /**
     * 修改新闻中心
     */
    //@PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "新闻中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TNews tNews)
    {
        return toAjax(tNewsService.updateTNews(tNews));
    }

    /**
     * 删除新闻中心
     */
    //@PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "新闻中心", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tNewsService.deleteTNewsByIds(ids));
    }
}
